﻿using System;
using System.Data.Linq;
using System.Linq.Expressions;

namespace Stiline.DAL.Repositories
{
    public class TaskRepository : BaseRepository<Entities.Task, DbContext.Task>
    {
        protected override Table<DbContext.Task> GetTable()
        {
            return Context.Tasks;
        }

        protected override Expression<Func<DbContext.Task, Entities.Task>> GetConverter()
        {
            var userRepository = new UserRepository();
            return t => new Entities.Task
                {
                    Id = t.Id,
                    Title = t.Title,
                    UserId = t.UserId,
                    User = userRepository.Get(t.UserId),
                    Description = t.Description,
                    StartTime =  t.StartTime,
                    EndTime = t.EndTime,
                    IsPublic = t.IsPublic
                };
        }

        protected override void UpdateEntry(DbContext.Task dbEntity, Entities.Task entity)
        {
            dbEntity.Id = entity.Id;
            dbEntity.Title = entity.Title;
            dbEntity.Description = entity.Description;
            dbEntity.UserId = entity.UserId;
            dbEntity.StartTime = entity.StartTime;
            dbEntity.EndTime = entity.EndTime;
            dbEntity.IsPublic = entity.IsPublic;
        }
    }
}
